﻿[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.ListControlValueHandler.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.ListControlValueHandler.debug.js",
                "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A control that binds the <see cref="LeafItem" /> control with a <see cref="ListBox" /> control.
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.ListControlValueHandler.js")]
    public class ListBoxValueHandler : ListControlValueHandler<ListBox>, IMultipleListControlValueHandler
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ListBoxValueHandler"/> class.
        /// </summary>
        public ListBoxValueHandler()
        {
            Helper = new MultipleListControlValueHandlerHelper(this);
        }        

        /// <summary>
        /// Gets or sets ListBox.
        /// </summary>
        /// <value>
        /// The list box.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ListBox ListBox
        {
            get
            {
                return Control;
            }

            set
            {
                Control = value;
            }
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public override string Value
        {
            get
            {
                return Helper.Value;
            }

            set
            {
                Helper.Value = value;
            }
        }

        /// <summary>
        /// Gets or sets ValueSeparator.
        /// </summary>
        [DefaultValue(null)]
        public string ValueSeparator
        {
            get;
            set;            
        }

        /// <summary>
        /// Gets the text associated with the selected item
        /// </summary>
        protected override string ViewModeControlValue
        {
            get
            {
                return Helper.ViewModeControlValue;
            }
        }

        private MultipleListControlValueHandlerHelper Helper
        {
            get;
            set;
        }

        /// <summary>
        /// Prepares the control, its css class and the child items.
        /// Adds value seprator to html and sets listbox selection mode to multiple if the value separator in non-empty.
        /// </summary>
        protected override void PrepareControl()
        {
            base.PrepareControl();
            if (!String.IsNullOrEmpty(ValueSeparator))
            {
                Control.SelectionMode = ListSelectionMode.Multiple;
                Control.Attributes["valueSeparator"] = ValueSeparator;
            }
        }
    }
}